package com.ft.adbix.core.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ft.adbix.core.R;
import com.ft.adbix.core.listener.InterstitialListener;
import com.ft.adbix.core.response.Ad;
import com.ft.adbix.core.widget.InteractionLayout;
import com.ft.adbix.utils.AdLogUtils;
import com.ft.adbix.utils.AdOpener;
import com.ft.adbix.utils.AdTrackerReporter;
import com.ft.adbix.utils.data.gather.util.device.DisplayUtil;
import com.ft.adbix.utils.universalimageloader.core.ImageLoader;

public class InterstitialPicAdView extends FrameLayout {

    private Ad adData;
    private ImageView ivClose;
    private FrameLayout layoutContainer;
    private InteractionLayout layoutInteraction;
    private InterstitialListener listener;
    private AdOpener adOpener;
    private AdTrackerReporter reporter;
    //0：上图下文  1：全图
    private int adPicStyle = 0;
    public static final int INTERSTITIAL_STYLE_TOP_PIC = 0;
    public static final int INTERSTITIAL_STYLE_PIC = 1;
    private Activity mActivity;

    public InterstitialPicAdView(@NonNull Activity context, int adPicStyle) {
        this(context, null);
        this.adPicStyle = adPicStyle;
    }

    public InterstitialPicAdView(@NonNull Activity context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public InterstitialPicAdView(@NonNull Activity context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mActivity = context;
    }

    public void renderView(Ad adData, InterstitialListener listener) {
        this.adData = adData;
        this.listener = listener;
        reporter = new AdTrackerReporter(adData);
        adOpener = new AdOpener(getContext(), adData, reporter);

        if (adPicStyle == INTERSTITIAL_STYLE_TOP_PIC) {
            createHalfPicView();
        } else {
            createAllPicView();
        }

    }

//    private void createTopPicView() {
//        LayoutInflater.from(getContext()).inflate(R.layout.adbix_interstitial_template_style_top_pic, this);
//        TextView tvTitle = findViewById(R.id.adbix_interstitial_tv_title);
//        TextView tvDesc = findViewById(R.id.adbix_interstitial_tv_desc);
//        layoutContainer = findViewById(R.id.adbix_interstitial_fl_container);
//        ivClose = findViewById(R.id.adbix_iv_close);
//        layoutInteraction = findViewById(R.id.adbix_rl_ad_interact);
//        layoutInteraction.setReporter(reporter);
//        tvTitle.setText(adData.getTitle());
//        tvDesc.setText(adData.getDesc());
//        ivClose.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (listener != null) {
//                    listener.onAdClose();
//                }
//                mActivity.finish();
//            }
//        });
//        layoutInteraction.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                AdLogUtils.d("click = " + reporter.getMarkInfo());
//                if (listener != null) {
//                    listener.onAdClick();
//                }
//                reporter.reportClick();
//                adOpener.openAd();
//            }
//        });
//
//        renderPic();
//    }

    private void createHalfPicView() {
        LayoutInflater.from(getContext()).inflate(R.layout.adbix_interstitial_template_style_pic, this);
        layoutContainer = findViewById(R.id.adbix_interstitial_fl_container);
        ivClose = findViewById(R.id.adbix_iv_close);
        layoutInteraction = findViewById(R.id.adbix_rl_ad_interact);
        layoutInteraction.setReporter(reporter);
        ivClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onAdClose();
                }
                mActivity.finish();
            }
        });
        layoutInteraction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AdLogUtils.d("click = " + reporter.getMarkInfo());
                if (listener != null) {
                    listener.onAdClick();
                }
                reporter.reportClick();
                adOpener.openAd();
            }
        });

        renderPic();
    }

    private void createAllPicView() {
        LayoutInflater.from(getContext()).inflate(R.layout.adbix_interstitial_template_style_pic, this);
        layoutContainer = findViewById(R.id.adbix_interstitial_fl_container);
        ivClose = findViewById(R.id.adbix_iv_close);
        layoutInteraction = findViewById(R.id.adbix_rl_ad_interact);
        layoutInteraction.setReporter(reporter);
        ivClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onAdClose();
                }
                mActivity.finish();
            }
        });
        layoutInteraction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AdLogUtils.d("click = " + reporter.getMarkInfo());
                if (listener != null) {
                    listener.onAdClick();
                }
                reporter.reportClick();
                adOpener.openAd();
            }
        });

        renderPic();
    }

    private void renderPic() {
        ImageView ivPic = new ImageView(getContext());
        ivPic.setAdjustViewBounds(true);
//        ivPic.setScaleType(ImageView.ScaleType.);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        int dis = DisplayUtil.dip2px(20);
        params.leftMargin = dis;
        params.topMargin = dis;
        params.rightMargin = dis;
        params.bottomMargin = dis;
        layoutContainer.addView(ivPic, params);

        ImageLoader.getInstance().displayImage(adData.getImageUrl(), ivPic);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        reporter.reportDisplay();
        if (listener != null) {
            listener.onAdShow();
        }
    }

}
